package com.ht.medicaltreatment.activity.haocai2

import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.RecipientListAdapter
import com.ht.medicaltreatment.api.ConsumableApprovalAPI
import com.ht.medicaltreatment.bean.SendProjectConsumableRecord
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.view.QianShouDialog
import com.ht.medicaltreatment.viewmodel.SendListViewModel
import kotlinx.android.synthetic.main.activity_send_list_layout.radioGroup
import kotlinx.android.synthetic.main.activity_send_list_layout.rbJ
import kotlinx.android.synthetic.main.activity_send_list_layout.rbS
import kotlinx.android.synthetic.main.activity_send_list_layout.recyclerView

class SendListActivity : BaseActivity(), OnItemChildClickListener {
    override fun getLayoutId() = R.layout.activity_send_list_layout

    var jList: MutableList<SendProjectConsumableRecord>? = null
    var sList: MutableList<SendProjectConsumableRecord>? = null
    private lateinit var adapter: RecipientListAdapter
    private val model by viewModels<SendListViewModel>()
    private lateinit var dialog: QianShouDialog
    override fun init() {
        setTitle("耗材快件")

        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = RecipientListAdapter()
        recyclerView.adapter = adapter
        adapter.setOnItemChildClickListener(this)
        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            if (checkedId == R.id.rbJ) {
                adapter.setNewInstance(jList)
            } else {
                adapter.setNewInstance(sList)
            }
        }
        initData()
        dialog = QianShouDialog(this)
    }

    fun initData() {
        rbJ.isChecked = true
        showProgress()
        model.requestList {
            dismissProgress()
            it.sendProjectConsumableRecordList.forEach {
                it.isSend = true
            }

            jList = it.sendProjectConsumableRecordList
            sList = it.recipientProjectConsumableRecordList
            adapter.setNewInstance(jList)
            rbJ.text = "寄件：${it.sendProjectConsumableRecordList.size}"
            rbS.text = "收件：${it.recipientProjectConsumableRecordList.size}"
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as SendProjectConsumableRecord
        ScanUtils.open {
            dialog.dismiss()
            val api = ConsumableApprovalAPI(
                sign = 4,
                id = item.id,
                expressUid = it
            )
            showProgress()
            model.approval(api){
                initData()
            }
        }
        dialog.show()
    }
}